package com.lwl.blog.system.service.impl;

import com.lwl.blog.core.exception.BaseException;
import com.lwl.blog.core.exception.UserNotFoundException;
import com.lwl.blog.system.dto.UserToken;
import com.lwl.blog.system.entity.SystemUserEntity;
import com.lwl.blog.system.service.ISystemUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 系统管理员登录业务层
 * 登录业务逻辑
 * </p>
 *
 * @author lwl
 * @since 22-12-26
 */

@Service
public class SysLoginSerivce {

    @Autowired
    private ISystemUserService systemUserService;

    @Autowired
    private TokenService tokenService;

    //登录方法
    public String login(String username, String password) {

        //1.判断用户是否存在
        SystemUserEntity systemUserEntity = systemUserService.getByUsername(username);
        if (systemUserEntity == null) {
            throw new UserNotFoundException(101, "用户名不存在!");
        }

        //2.密码验证
        if (!password.equals(systemUserEntity.getPassword())) {
            throw  new BaseException(101,"密码错误!");
        }

        //3.创建token对象，基于systemUserEntity创建一个userToken对象
        //把用户的登录信息全部封装到userToken对象中
        UserToken userToken = tokenService.createToken(systemUserEntity);

        //4.将token令牌返回给controller
        return userToken.getToken();
    }


}
